package xj.graph2d.template;

import java.util.Map;

import xj.graph2d.DrawAttr;
import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeBase;
import xj.graph2d.EllipseShape;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.LineShape;
import xj.graph2d.OpenShape;
import xj.graph2d.RectangleShape;
import xj.graph2d.shapes.CircleShape;
import xj.graph2d.shapes.CompartmentalRectangleShape;
import xj.graph2d.shapes.CubicCurveShape;
import xj.graph2d.shapes.OrthogonalLineShape;
import xj.graph2d.shapes.PolyCurveShape;
import xj.graph2d.shapes.PolyLineShape;
import xj.graph2d.shapes.PolygonShape;
import xj.graph2d.shapes.QuadCurveShape;
import xj.graph2d.shapes.RectangularShape;
import xj.graph2d.shapes.RoundRectangleShape;
import xj.graph2d.shapes.ShapeConstants;

public class BasicTemplate extends Template {

  static String[] names = {
      // open shapes
      "Line", "QuadCurve", "CubicCurve", "PolyLine", "PolyCurve", "Orthline",

      // closed shapes
      "Triangle", "RightTriangle", "Diamond", "Rectangle", "Ellipse",
      "RoundRectangle", "Circle", "Pentagon", "Hexagon", "Octagon", "Polygon",
      // "3-Compartment",
  };

  public BasicTemplate() {
  }

  @Override
  public void makeTemplate(Map iconMap) {
    for (int i = 0; i < names.length; i++) {
      // addButton(names[i], makeShape(names[i]), 0);
      DrawShape shape = makeShape(names[i]);
      int iconWidth = 36, iconHeight = 30;
      if (shape instanceof OpenShape) { 
	iconHeight = 24; 
      }
      add(new TemplateToolButton(names[i], shape, iconWidth, iconHeight, false, true));
    }
  }

  protected DrawShapeBase makeShape(String name) {
    DrawShapeBase shape = null;
    if ("Line".equals(name)) {
      shape = new LineShape(0, 0, 100, 100);
    } else if ("QuadCurve".equals(name)) {
      shape = new QuadCurveShape(0, 0, 100, 100, 100, 0);
    } else if ("CubicCurve".equals(name)) {
      shape = new CubicCurveShape(0, 20, 100, 80, 40, 0, 60, 100);
    } else if ("Rectangle".equals(name)) {
      shape = new RectangleShape(0, 0, 100, 80);
    } else if ("Ellipse".equals(name)) {
      shape = new EllipseShape(0, 0, 100, 80);
    } else if ("RoundRectangle".equals(name)) {
      shape = new RoundRectangleShape(0, 0, 100, 80);
    } else if ("Circle".equals(name)) {
      shape = new CircleShape(50, 50, 50);
    } else if ("Triangle".equals(name)) {
      shape = new RectangularShape(ShapeConstants.TRIANGLE_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 70);
    } else if ("RightTriangle".equals(name)) {
      shape = new RectangularShape(ShapeConstants.RIGHT_TRIANGLE_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 80);
    } else if ("Diamond".equals(name)) {
      shape = new RectangularShape(ShapeConstants.DIAMOND_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 100);
    } else if ("Pentagon".equals(name)) {
      shape = new RectangularShape(ShapeConstants.PENTAGON_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 100);
    } else if ("Hexagon".equals(name)) {
      shape = new RectangularShape(ShapeConstants.HEXAGON_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 100);
    } else if ("Octagon".equals(name)) {
      shape = new RectangularShape(ShapeConstants.OCTAGON_SHAPE,
				   ShapeConstants.NORTH, 0, 0, 100, 100);
    } else if ("3-Compartment".equals(name)) {
      float[] divs = new float[] { 0.2f, 0.4f };
      shape = new CompartmentalRectangleShape(0, 0, 80, 100, divs);
      shape.setAllowRotation(false);
      shape.setSymetricResize(false);
    } else if ("Polygon".equals(name)) {
      PolygonShape polygon = new PolygonShape(50, 0);
      polygon.addPoint(100, 30);
      polygon.addPoint(80, 100);
      polygon.addPoint(20, 100);
      polygon.addPoint(0, 30);
      polygon.calculateBounds();
      shape = polygon;
    } else if ("PolyLine".equals(name)) {
      PolyLineShape polyline = new PolyLineShape(50, 0);
      polyline.addPoint(0, 25);
      polyline.addPoint(0, 70);
      polyline.addPoint(50, 100);
      polyline.calculateBounds();
      shape = polyline;
    } else if ("PolyCurve".equals(name)) {
      PolyCurveShape polycurve = new PolyCurveShape(60, 0);
      // polycurve.addPoint(0, 25);
      polycurve.addPoint(0, 50);
      polycurve.addPoint(100, 100);
      polycurve.adjustControlPoints();
      polycurve.calculateBounds();
      shape = polycurve;
    } else if ("Orthline".equals(name)) {
      OrthogonalLineShape orthline = new OrthogonalLineShape(50, 0);
      orthline.addPoint(25, 25);
      orthline.addPoint(0, 50);
      orthline.addPoint(25, 75);
      orthline.addPoint(50, 100);
      orthline.calculateBounds();
      shape = orthline;
    }
    DrawAttr attr = new DrawAttr();
    shape.setDrawAttr(attr);
    setPenColor(shape, NODE_FG);
    if (shape instanceof GraphNodeBase) { 
      setNodeAttr(shape);
      setNodeColor(shape, NODE_FG, NODE_BG);
    }
    return shape;
  }

  public static final String NODE_FG = "template.Basic.fg";
  public static final String NODE_BG = "template.Basic.bg";

}
